package com.couldsail.takeaway.controller.user;


import com.couldsail.takeaway.common.result.Result;
import com.couldsail.takeaway.constant.DefaultNumberConstant;
import com.couldsail.takeaway.constant.ResultMessageConstant;
import com.couldsail.takeaway.context.BaseContext;
import com.couldsail.takeaway.pojo.entity.AddressBook;
import com.couldsail.takeaway.pojo.vo.AddressBookVO;
import com.couldsail.takeaway.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController("UserAdressBookContr")
@RequestMapping("/user/addressBook")
@Api(tags = "地址管理")
@RequiredArgsConstructor
@CrossOrigin(origins = "*") // 允许所有来源的请求跨域
public class AddressBookController {
    private final AddressBookService addressBookService;
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBookVO>> list(){
      log.info("查询当前登录用户的所有地址信息");
        List<AddressBookVO> list = addressBookService.userList();
        return Result.success(list);
    }
    /**
     * 插入地址
     * @param addressBookVO
     */
    @ApiOperation("插入地址")
    @PostMapping("/insert")
    public Result<String> insert(@RequestBody  AddressBookVO addressBookVO){
        log.info("新增地址{}",addressBookVO);
        addressBookService.insertAddressBook(addressBookVO);
        return Result.success(ResultMessageConstant.INSERT_ADDRESS);
    }

    /**
     * 依据id查询地址
     * @param id
     * @return
     */
    @ApiOperation("依据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBookVO> getById(@PathVariable Long id){
        log.info("依据id：{}查询地址",id);
        AddressBook byId = addressBookService.getById(id);
        AddressBookVO addressBookVO = new AddressBookVO();
        BeanUtils.copyProperties(byId,addressBookVO);
        addressBookVO.setIsDefault(Objects.equals(byId.getIsDefault(), DefaultNumberConstant.ADDRESS_IS_DEFAULT));
        return Result.success(addressBookVO);
    }

    /**
     * 设置默认值
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认值")
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody  AddressBook addressBook){
        log.info("设置默认值{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success(ResultMessageConstant.SET_DEFAULT_ADDRESS);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @ApiOperation("修改地址")
    @PutMapping
    public Result<String> updateAddress(@RequestBody AddressBookVO addressBook){
        log.info("修改地址{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success(ResultMessageConstant.UPDATE_SUCCESS);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @ApiOperation("删除地址")
    @GetMapping("/del")
    public Result<String> deleteAddress(Long id){
        log.info("删除地址id:{}",id);
        addressBookService.deleteAddress(id);
        return Result.success(ResultMessageConstant.DEL_ADDRESS);

    }

    /**
     * 获取默认地址
     * @return
     */
    @ApiOperation("获取默认地址")
    @GetMapping("/default")
    public Result<AddressBookVO> getDefaultAddress(){
        log.info("获取默认地址");
        return Result.success(addressBookService.getDefaultAddress());
    }
}
